package com.swn.common.util;

import java.nio.charset.Charset;
import java.util.Properties;
 
/**
 * Java简单的加密解密算法，使用异或运算
 * 
 * @author Leon
 * @Date 2016年6月15日  下午7:07:53
 * @Note <这里是该类的使用注意事项>

 */
public class DeEnCode {  
 
   private static final String key0 = "P@ssW0rd";  
   private static final Charset charset = Charset.forName("UTF-8");  
    private static byte[] keyBytes = key0.getBytes(charset);  
      
    public static String encode(String enc){  
        byte[] b = enc.getBytes(charset);  
        for(int i=0,size=b.length;i<size;i++){  
            for(byte keyBytes0:keyBytes){  
                b[i] = (byte) (b[i]^keyBytes0);  
            }  
        }  
        return new String(b);  
    }  
      
    public static String decode(String dec){  
        byte[] e = dec.getBytes(charset);  
        byte[] dee = e;  
        for(int i=0,size=e.length;i<size;i++){  
            for(byte keyBytes0:keyBytes){  
                e[i] = (byte) (dee[i]^keyBytes0);  
            }  
        }  
        return new String(e);  
    }  
  
    public static void main(String[] args) {  
       /* String dec = decode(enc);  
        System.out.println(enc);  
        System.out.println(dec);  */
        
		
		try {
			Properties properties = new Properties();
			
			String user = "socialorg";
			String password = "socialorg";
			String driverClass = "oracle.jdbc.driver.OracleDriver";
			String jdbcUrl = "jdbc:oracle:thin:@192.168.2.88:1521:orcl";
			
			
			properties.put("user", encode(user));
			properties.put("password", encode(password));
			properties.put("driverClass", encode(driverClass));
			properties.put("jdbcUrl", encode(jdbcUrl));
			
			
			//file路径
			String path = StaticParam.getClassesPath()+"db.bat";
			SerializableUtil.writeObjToFile(properties, path);
			Properties readProperties = (Properties)SerializableUtil.readObjFromFile(path);
			
			
			String decUser = decode(readProperties.getProperty("user"));
			String decPassword = decode(readProperties.getProperty("password"));
			String decDriverClass = decode(readProperties.getProperty("driverClass"));
			String decJdbcUrl = decode(readProperties.getProperty("jdbcUrl"));
			
			System.out.println(decUser);
			System.out.println(decPassword);
			System.out.println(decDriverClass);
			System.out.println(decJdbcUrl);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    }  
}  
